//
//  LZNewResetPasswordViewController.m
//  laziz_Merchant
//
//  Created by xbnzd on 17/3/16.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZNewResetPasswordViewController.h"
#import "UIViewController+NavigationItemSetting.h"
#import "LZUserInfoPresenter.h"
#import "UIView+Rotation.h"
#import "LZRemindView.h"
#import "extobjc.h"

@interface LZNewResetPasswordViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *logoTopToSuperViewCST;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *subTitleLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tfTopToSubTitleLabelCST;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIButton *showOrHiddenPasswordBtn;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *submitBtnBottomToSuperViewCSt;


@property (strong, nonatomic) LZUserInfoPresenter *userInfoPresenter;
@end

@implementation LZNewResetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configureUI];
    
    [self configNavigationBar];
}

- (BOOL)fd_prefersNavigationBarHidden {
    return YES;
}

- (void)navbarBackButtonClickAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - private
- (void)resetPasswordByEmail
{
    NSString * passwordStr = [NSString stringWithFormat:@"%@", self.passwordTF.text];
    if (passwordStr.length <= 0) {
        [LZRemindView initWithImage:[UIImage imageNamed:@"错误提示"]
                         andContent:LZLocalizedString(@"enter_password", @"")];
        return;
    } else if (passwordStr.length > 0 && passwordStr.length < 6) {
        [LZRemindView initWithImage:[UIImage imageNamed:@"错误提示"]
                         andContent:LZLocalizedString(@"enter_password_6_chart", @"")];
        return;
    }
    
    [self startLoadingViewForView:self.view loadingViewUserInteractionEnabled:YES];
    
    @weakify(self);
    [self.userInfoPresenter userResetPasswordForEmail:self.email Password:passwordStr Success:^(id data) {
        
        @strongify(self);
        [self stopLoadingViewForView:self.view];
        DLog(@"重置密码成功:\r%@", data);
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    } Failure:^(NSString *fail) {
        @strongify(self);
        [self stopLoadingViewForView:self.view];
        DLog(@"重置密码失败:\r%@", fail);
        [LZRemindView initWithImage:[UIImage imageNamed:@"错误提示"]
                         andContent:LZLocalizedString(@"reset_password_fail", @"")];
    }];
}

- (void)resetPasswordForPhone
{
    NSString * passwordStr = [NSString stringWithFormat:@"%@", self.passwordTF.text];
    if (passwordStr.length <= 0) {
        [LZRemindView initWithImage:[UIImage imageNamed:@"错误提示"]
                         andContent:LZLocalizedString(@"enter_password", @"")];
        return;
    } else if (passwordStr.length > 0 && passwordStr.length < 6) {
        [LZRemindView initWithImage:[UIImage imageNamed:@"错误提示"]
                         andContent:LZLocalizedString(@"enter_password_6_chart", @"")];
        return;
    }
    
    [self startLoadingViewForView:self.view loadingViewUserInteractionEnabled:YES];
    
    @weakify(self);
    [self.userInfoPresenter userResetPasswordWithMoblieNum:self.cellPhone andVerifiCode:self.verificationStr andPassword:passwordStr andonSuccess:^(id data) {
        
        @strongify(self);
        [self stopLoadingViewForView:self.view];
        DLog(@"重置密码成功:\r%@", data);
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    } onFail:^(NSString *fail) {
        @strongify(self);
        [self stopLoadingViewForView:self.view];
        DLog(@"重置密码失败:\r%@", fail);
        [LZRemindView initWithImage:[UIImage imageNamed:@"错误提示"]
                         andContent:LZLocalizedString(@"reset_password_fail", @"")];
    }];
    
}

#pragma mark btn action
- (IBAction)showOrHiddenPasswordBtnAction:(id)sender {
    
    self.showOrHiddenPasswordBtn.selected = !self.showOrHiddenPasswordBtn.isSelected;
    if (self.showOrHiddenPasswordBtn.isSelected) {
        self.passwordTF.secureTextEntry  = NO;
    }else{
        self.passwordTF.secureTextEntry  = YES;
    }
}
- (IBAction)submitBtnAction:(id)sender {
   
    if (self.isResetByPhone) {
        [self resetPasswordForPhone];
    }else{
        [self resetPasswordByEmail];
    }
}


#pragma mark - UI
- (void)configureUI
{
    self.umLogPageViewName = @"重设密码";
    self.view.backgroundColor = UIColorFromRGB(0x333333, 1);
    self.titleLabel.text = LZLocalizedString(@"password_recovery", nil);
    self.subTitleLabel.text = LZLocalizedString(@"recovery_reset_password", nil);
    _userInfoPresenter = [[LZUserInfoPresenter alloc] init];
    
    [self lz_setCustomBarAlpha:0];
    
    self.submitBtn.layer.cornerRadius = 22.;
    self.submitBtn.layer.masksToBounds = YES;
    
    GET_SCREEN_SCALE(scale);
    self.passwordTF.secureTextEntry = YES;
    self.passwordTF.font = [UIFont systemFontOfSize:17*scale];
    self.passwordTF.placeholder = LZLocalizedString(@"enter_new_password", nil);
    [self.passwordTF setTintColor:UIColorFromRGB(0x33a928, 1)];
    [self.passwordTF setValue:UIColorFromRGB(0x999999, 1) forKeyPath:@"_placeholderLabel.textColor"];
    
    self.logoTopToSuperViewCST.constant = 79*scale;
    self.tfTopToSubTitleLabelCST.constant = 96 *scale;
    self.submitBtnBottomToSuperViewCSt.constant = 150 *scale;
}

- (void)configNavigationBar{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 60, 40);
    [btn addTarget:self action:@selector(navbarBackButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    UIImage *image_back = [UIImage imageNamed:@"lz_back_en"];
    [btn setImage:image_back forState:UIControlStateNormal];
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(btn.bounds)-image_back.size.width);
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
        [btn setLanguageRotaion];
    }
    
    [self hiddenBarNavigationItemSetting:@[btn] type:NAVIGATIONITEMSETTING_LEFT];
    [self lz_setCustomBarAlpha:0];
}


@end
